<template>
    <el-card>
        <div class="header" slot="header">
            <span>修改商品</span>
        </div>

        <!-- 表单 -->
        <el-form ref="form" :model="form" label-width="80px">
            <!-- 商品名称 -->
            <el-form-item label="商品名称">
                <el-input v-model="form.name" placeholder="商品名称"></el-input>
            </el-form-item>

            <!-- 商品分类 -->
            <el-form-item label="商品分类">
                <el-select v-model="form.category" placeholder="请选择商品分类">
                <el-option v-for="(v, i) in classList" :label="v" value="v" :key="i"></el-option>
                </el-select>
            </el-form-item>

            <!-- 商品价格 -->
            <el-form-item label="商品价格">
                <el-input-number v-model="form.price" :min="1" :max="99999"></el-input-number>
            </el-form-item>

            <!-- 商品图片 -->
            <el-form-item label="商品图片">
                <el-upload
                    class="avatar-uploader"
                    action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
                    :show-file-list="false"
                    :on-success="success"
                    :before-upload="beforeUpload">
                    <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <!-- 商品描述 -->
            <el-form-item label="商品描述">
                <el-input type="textarea" v-model="form.goodsDesc"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="goodsEdit">立即修改</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import {categories, goodsEdit} from "@/api/goods.js";  //引入账号模块接口
export default {
    data() {
        return {
            form: {
                name: '',
                category: '',
                price: 1,
                imgUrl: "",
                goodsDesc: "",
            },
            classList:[],  //商品分类
        }
    },

    async created(){
        let res = await categories();
        this.classList = res.data;  //商品分类列表
        this.form = JSON.parse(this.$route.query.row);
    },

    methods: {
        // 立即修改按钮
        async goodsEdit() {
            let res = await goodsEdit(this.form);
            if(res.data.code === 0){
                this.$router.push("/goods/list");
            }
        },

        // 头像上传成功后执行的回调函数
        success(res, file) {
            if(res.code === 0){
                this.form.imgUrl = res.imgUrl;
            }
        },

        // 头像上传之前只执行的回调函数
        beforeUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    },
}
</script>

<style lang="less" scoped>
.el-form{
    width: 300px;
}
:deep(.avatar-uploader .el-upload) {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
}
.avatar {
    width: 80px;
    height: 80px;
    display: block;
}
.avatar-uploader{
    display: inline-block;
    vertical-align: middle;
}
</style>